package impl

import (
	"adam2/internal/dto"
	"adam2/internal/model"
	"adam2/internal/vo"
	pConstants "anubis-framework/pkg/constants"
	pDomain "anubis-framework/pkg/domain"
	"anubis-framework/pkg/io"
	"anubis-framework/pkg/util"
)

type ConceptBoardIndexServiceImpl struct {
	*BaseServiceImpl
}

func GetConceptBoardIndexServiceImpl() *ConceptBoardIndexServiceImpl {
	return &ConceptBoardIndexServiceImpl{GetBaseServiceImpl()}
}

// 获取最近记录
func (_conceptBoardIndexServiceImpl ConceptBoardIndexServiceImpl) FindLatestConceptBoardIndex() *pDomain.ServiceResult {
	io.Infoln("获取最近记录")

	var conceptBoardIndex model.ConceptBoardIndex = _conceptBoardIndexServiceImpl._conceptBoardIndexDaoImpl.FindLatestConceptBoardIndex()
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = conceptBoardIndex
	return serviceResult
}

// 查询前一个交易日的日期
func (_conceptBoardIndexServiceImpl ConceptBoardIndexServiceImpl) PreviousDate(transactionDate string) *pDomain.ServiceResult {
	io.Infoln("查询前一个交易日的日期")

	var previousDate string = _conceptBoardIndexServiceImpl._conceptBoardIndexDaoImpl.PreviousTransactionDate(transactionDate)
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = previousDate
	return serviceResult
}

// 查询后一个交易日的日期
func (_conceptBoardIndexServiceImpl ConceptBoardIndexServiceImpl) NextTransactionDate(transactionDate string) *pDomain.ServiceResult {
	io.Infoln("查询后一个交易日的日期")

	var nextDate string = _conceptBoardIndexServiceImpl._conceptBoardIndexDaoImpl.NextTransactionDate(transactionDate)
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = nextDate
	return serviceResult
}

// 根据开始时间和结束时间，计算平均KD值
func (_conceptBoardIndexServiceImpl ConceptBoardIndexServiceImpl) FindAverageKDByBeginDateAndEndDate(kdVo *vo.KDVo) *pDomain.ServiceResult {
	io.Infoln("根据开始时间和结束时间，计算平均KD值")

	var conceptBoardIndexArray model.ConceptBoardIndexArray = _conceptBoardIndexServiceImpl._conceptBoardIndexDaoImpl.FindAverageKDByBeginDateAndEndDate(kdVo.BeginTime, kdVo.EndTime)
	var averageKdDto dto.KdDto = dto.KdDto{}
	if conceptBoardIndexArray != nil && len(conceptBoardIndexArray) != 0 {
		var transactionDateArray []string
		var kArray []float64
		var dArray []float64
		for _, conecptBoardIndex := range conceptBoardIndexArray {
			transactionDateArray = append(transactionDateArray, util.DateToString(conecptBoardIndex.TransactionDate))
			kArray = append(kArray, conecptBoardIndex.K)
			dArray = append(dArray, conecptBoardIndex.D)
		}
		averageKdDto.TransactionDateArray = transactionDateArray
		averageKdDto.KArray = kArray
		averageKdDto.DArray = dArray
	}
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = averageKdDto
	return serviceResult
}

// 根据开始时间和结束时间，查询某个概念板块的KD值
func (_conceptBoardIndexServiceImpl ConceptBoardIndexServiceImpl) FindKdByMyquantCodeAndBeginDateAndEndDate(kdVo *vo.KDVo) *pDomain.ServiceResult {
	io.Infoln("根据开始时间和结束时间，查询某个概念板块的KD值")

	var conceptBoardIndexArray model.ConceptBoardIndexArray = _conceptBoardIndexServiceImpl._conceptBoardIndexDaoImpl.FindKDByMyquantCodeAndBeginDateAndEndDate(kdVo.MyquantCode, kdVo.BeginTime, kdVo.EndTime)
	var averageKdDto dto.KdDto = dto.KdDto{}
	if conceptBoardIndexArray != nil && len(conceptBoardIndexArray) != 0 {
		var transactionDateArray []string
		var kArray []float64
		var dArray []float64
		for _, conecptBoardIndex := range conceptBoardIndexArray {
			transactionDateArray = append(transactionDateArray, util.DateToString(conecptBoardIndex.TransactionDate))
			kArray = append(kArray, conecptBoardIndex.K)
			dArray = append(dArray, conecptBoardIndex.D)
		}
		averageKdDto.TransactionDateArray = transactionDateArray
		averageKdDto.KArray = kArray
		averageKdDto.DArray = dArray
	}
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = averageKdDto
	return serviceResult
}

// 根据myquant_code、begin_date和end_date，查询记录，并按照transaction_date列升序排列
func (_conceptBoardIndexServiceImpl ConceptBoardIndexServiceImpl) FindByMyquantCodeAndBeginDateAndEndDateOrderByTransactionDateAsc(findConceptBoardIndexVo *vo.FindConceptBoardIndexVo) *pDomain.ServiceResult {
	io.Infoln("根据myquant_code、begin_date和end_date，查询记录，并按照transaction_date列升序排列")

	var conceptBoardIndexArray model.ConceptBoardIndexArray = _conceptBoardIndexServiceImpl._conceptBoardIndexDaoImpl.FindByMyquantCodeAndBeginDateAndEndDateOrderByTransactionDateAsc(findConceptBoardIndexVo.MyquantCode, findConceptBoardIndexVo.BeginDate, findConceptBoardIndexVo.EndDate)
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = conceptBoardIndexArray
	return serviceResult
}
